package com.qzsoft.zhjd.machine.modules.machinereport.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.system.modules.permission.dept.dao.SysDeptMapper;
import com.qzsoft.system.repository.sys.entity.SysDeptEntity;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.machine.modules.machinereport.dto.MachineReportIntactRateDto;
import com.qzsoft.zhjd.machine.modules.machinereport.mapper.MachineReportIntactRateMapper;
import com.qzsoft.zhjd.machine.modules.machinereport.service.MachineReportIntactRateService;

/**
 * @ClassName MachineReportIntactRateServiceImpl
 * @Description 设备完好率业务接口实现
 * @author linhuawei
 * @since 2021-11-04
 */
@Service
public class MachineReportIntactRateServiceImpl implements MachineReportIntactRateService {
    
    @Autowired
    private MachineReportIntactRateMapper machineReportIntactRateMapper;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private SysDeptMapper sysDeptDao;

    @Override
    public MachineReportIntactRateDto count(Map<String, Object> params) {
        // 判断是否有机构主键
        UserDetail user = baseServiceComponent.basegetLoginUser();
        String deptId = user.getDeptId();
        if (null != params.get("deptId")) {
            String temp = params.get("deptId").toString();
            if (temp.startsWith(deptId)) {
                deptId = temp;
            }
        }
        params.put("deptId", deptId);

        // 获取当前机构
        SysDeptEntity dept = sysDeptDao.getRealId(deptId);
        MachineReportIntactRateDto result = machineReportIntactRateMapper.count(params);
        if(null == result) {
            result = new MachineReportIntactRateDto();
            result.setBasicIntact(0);
            result.setIntact(0);
            result.setNormal(0);
            result.setRepaired(0);
            result.setScrapped(0);
        }
        
        // 设置单位信息
        result.setDeptId(dept.getId());
        result.setDeptPkid(dept.getPkid());
        result.setDeptName(dept.getName());

        // 获取下级机构
        QueryWrapper<SysDeptEntity> query = new QueryWrapper<>();
        query.eq("pid", deptId).orderByAsc("sort", "id");
        List<SysDeptEntity> deptList = sysDeptDao.selectList(query);
        List<MachineReportIntactRateDto> list = null;
        if (ObjectUtils.isNotEmpty(deptList)) {
            MachineReportIntactRateDto dto = null;
            list = new ArrayList<MachineReportIntactRateDto>();
            for (int i = 0, len = deptList.size(); i < len; i++) {
                dept = deptList.get(i);
                if("4".equals(dept.getType())) {
                    continue;// 磅房类型不参与统计
                }
                params.put("deptId", dept.getId());
                dto = machineReportIntactRateMapper.count(params);
                if(null == dto) {
                    dto = new MachineReportIntactRateDto();
                    dto.setBasicIntact(0);
                    dto.setIntact(0);
                    dto.setNormal(0);
                    dto.setRepaired(0);
                    dto.setScrapped(0);
                }
                dto.setDeptId(dept.getId());
                dto.setDeptPkid(dept.getPkid());
                dto.setDeptName(dept.getName());
                dto.setList(null);
                list.add(dto);
            }
        }
        result.setList(list);

        // 返回结果
        return result;
    }
    
}
